﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using WPFExcelReport.Helpers;
namespace WPFExcelReport.ViweModels
{
    public class ITXTRANSLATEViewModel:BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        private Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public string Mandant
        {
            get
            {
                return app.Mandant;
            }
        }
        public string company
        {
            get
            {
                return app.company;
            }
        }
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
 
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public ITXTRANSLATEViewModel()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }    
        }

        public DataSet LoadITXMULLANG(string client)
        { 
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = SetEnv.ICC_MULLANG.NewRow();
                dwData["MANDANT"] = client;
                setResult = INTEXDBC.QueryICC_MULLANG(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveMULLANG(DataSet src, string client)
        {
            try
            {
                foreach (DataRow rw in src.Tables["ICC_MULLANG"].Rows)
                {
                    rw["MANDANT"] = client;
                    INTEXDBC.SaveICC_MULLANG(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void ProcessFiles(string file)
        {

            if (file.ToString() == "")
                return;
            string sourceFile = file;
            string destinationFile = file;
            Dictionary<int, string> lines2replace = new Dictionary<int, string>();
            // Read the appropriate line from the file.
            string lineToWrite = null;
            System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;
            string[] lines = File.ReadAllLines(sourceFile);

            using (StreamReader reader = new StreamReader(sourceFile))
            {
                for (int i = 1; i <= lines.Length; ++i)
                {
                    int idx = -1, idx2 = -1, idx1 = -1;
                    int startidx = -1;
                    string line2write;
                    string str = reader.ReadLine();
                    idx1 = str.IndexOf("Word="); if (idx1 > 0) continue;
                    idx = str.IndexOf("Content=");
                    if (idx > 0)
                    {
                        startidx = idx + 9;
                    }
                    else
                    {
                        if (idx == -1)
                            idx = str.IndexOf("Text=");
                        if (idx > 0)
                        {
                            idx1 = str.IndexOf("Binding"); if (idx1 > 0) continue;
                            idx1 = str.IndexOf("TextBox"); if (idx1 > 0) continue;
                            idx1 = str.IndexOf("ComboBox"); if (idx1 > 0) continue;
                            startidx = idx + 6;
                        }
                        else
                        {
                            if (idx == -1)
                                idx = str.IndexOf("Header=");
                            if (idx > 0)
                            {
                                startidx = idx + 8;
                            }
                            else
                            {
                                if (idx == -1)
                                    idx = str.IndexOf("ToolTip=");
                                if (idx > 0)
                                {
                                    idx1 = str.IndexOf("Binding"); if (idx1 > 0) continue;
                                    startidx = idx + 9;
                                }
                            }
                        }
                    }
                    if (idx == -1) continue;
                    idx2 = str.IndexOf("\"", startidx);
                    string content = str.Substring(startidx, idx2 - startidx);
                    if (content.Trim() == "") continue;
                    lineToWrite = str.Substring(0, startidx) + "{Binding Source={Localization:Translate Word=" + content + "}}" + str.Substring(idx2);
                    lines2replace.Add(i, lineToWrite);
                }
            }

            lines = File.ReadAllLines(destinationFile);

            // Write the new file over the old file.
            using (StreamWriter writer = new StreamWriter(destinationFile))
            {
                for (int currentLine = 1; currentLine <= lines.Length; ++currentLine)
                {
                    if (lines2replace.ContainsKey(currentLine))
                    {
                        lineToWrite = lines2replace[currentLine];
                        writer.WriteLine(lineToWrite);
                    }
                    else
                    {
                        writer.WriteLine(lines[currentLine - 1]);
                    }
                }
            }
        }
        public void GetFiles(string root)
        {

            List<string> dirs = new List<string>(Directory.EnumerateDirectories(root));
            List<string> files;
            TextFileReader reader1 = null;
            System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;

            foreach (var dir in dirs)
            {
                files = new List<string>(Directory.EnumerateFiles(dir));
                foreach (var file in files)
                {
                    if (!file.EndsWith(".xaml")) continue;
                    if (file.EndsWith("App.xaml")) continue;
                    EventDispatcher.Instance.RaiseStatus("开始处理" + file, StatusEventType.StopProgress);
                    ProcessFiles(file);
                    reader1 = new TextFileReader(file, ",");
                    string prog = "";
                    foreach (var text in reader1)
                    {
                        int idx = -1, idx2 = -1, idx1 = -1;
                        string str = text[0];
                        string content = "";
                        bool IsFound = false;
                        idx = str.IndexOf("x:Class=");
                        if (idx > 0)
                        {
                            idx = str.IndexOf("WPFExcelReport.");
                            if (idx > 0)
                            {
                                //<local:DocumentBase x:Class="WPFExcelReport.YCAWDPLAN"
                                idx2 = str.Length;
                                prog = str.Substring(idx + 14 + 1, idx2 - idx - 14 - 2);
                            }
                        }
                        idx = -1;
                        idx = str.IndexOf("Word=");
                        if (idx == -1) continue;

                        idx2 = str.IndexOf("}}", idx);
                        content = str.Substring(idx + 5, idx2 - idx - 5);
                        IsFound = true;

                        if (IsFound)
                        {
                            Insert2DB(content, prog);
                        }
                    }
                }
            }
        }
        public void Insert2DB(string str, string loc)
        {
            DataSet SResult = new DataSet();
            DataRow dwData = SetEnv.ICC_MULLANG.NewRow();
            dwData["MANDANT"] = "00";
            dwData["ROWNID"] = "0";
            dwData["MLKEY"] = ConvertGB2312To8859P1(str);
            dwData["MLLOC"] = loc;
            dwData["SCN"] = dwData["MLKEY"];
            dwData["EN"] = dwData["MLKEY"];
            SResult = INTEXDBC.QueryICC_MULLANG(dwData, 3);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                INTEXDBC.SaveICC_MULLANG(dwData);
            }
        }
  
        public void GetAllText2Translate(string root)
        { 
            root = @"D:\ITXRPRT\Reports\"+app.company;
            GetFiles(root);  
        }
    }
}
